package com.vvlive.caibao.smartcity.message;

import android.annotation.SuppressLint;
import android.os.Parcel;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * <b>群组邀请通知</b> -- 张三(30029)邀请你加入群组大神一群(899920),<br>
 * 				  		附加信息：快来快来数一数，二四六七八<br>
 * 				 		<b>params:</b> senderName, senderId, groupName, groupId, extra; tag == 0<br><br>
 * 
 * <b>群组接受通知</b> -- 李四(73830)同意加入群组大神一群<br>
 * 				 		<b>params:</b> senderName, senderId, groupName, null, null; tag == 1<br><br>
 * 
 * 
 * <b>申请加入群组</b> -- 李四(73830)申请加入群组大神一群<br>
 * 				  		附加信息：我擦<br>
 * 				 		<b>params:</b> senderName, senderId, null, null, extra; tag == -1<br><br>
 * 
 * <b>踢出群组通知</b> -- 你已经被踢出群组大神一群<br>
 * 						<b>params:</b> null, null, null, null, message, tag == -1<br></br>
 * 
 * @author zhj
 *
 */
@SuppressLint("ParcelCreator")
@MessageTag(value = "RC:GroupNotifiMsg")//, flag = MessageTag.ISPERSISTED |  MessageTag.ISCOUNTED
public class GroupNotification extends MessageContent {
	
	/** 群组邀请 */
	public static final int INVITATION = 0;	
	/** 群组邀请被接受 */
	public static final int INVITE_ACCEPTED = 1;
	/** 申请加入群组 */
	public static final int INVITE_APPLY = -1;		
	/** 被踢出群组 */
	public static final int KICKED_OUT = -2;
	
    private String groupId;
    private String message;
    private int tag;

    /**
     * 构造方法
     * @param groupId	群组ID
     * @param message	邀请信息
     * @param tag		标记：0-> 群组邀请，1->同意加入群组， 2->申请加入群组， 3-> 被踢出群组
     */
    public GroupNotification(String groupId, String message, int tag) {
        this.groupId = groupId;
        this.message = message;
        this.tag = tag;
    }

    public GroupNotification(byte[] data) {
        String jsonStr = null;

        try {
            jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
        	e1.printStackTrace();
        }

        try {
            JSONObject jsonObj = new JSONObject(jsonStr);
            setGroupId(jsonObj.getString("groupId"));
            setMessage(jsonObj.getString("message"));
            setTag(jsonObj.getInt("tag"));

        } catch (JSONException e) {
            Log.e("JSONException", e.getMessage());
        }

    }


    @Override
    public byte[] encode() {

        JSONObject jsonObj = new JSONObject();
        try {

            jsonObj.put("groupId", groupId);
            jsonObj.put("message", message);
            jsonObj.put("tag", tag);

        } catch (JSONException e) {
            Log.e("JSONException", e.getMessage());
        }

        try {
            return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

	public int getTag() {
		return tag;
	}

	public void setTag(int tag) {
		this.tag = tag;
	}


}
